<?php
require_once '../includes/functions.php';

// 如果已经登录，直接跳转到管理后台
if (is_logged_in()) {
    header('Location: /admin/');
    exit();
}

// 处理登录请求
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
    if (login($username, $password)) {
        header('Location: /admin/');
        exit();
    } else {
        header('Location: /?error=1');
        exit();
    }
} else {
    // 如果不是POST请求，重定向到首页
    header('Location: /');
    exit();
}
